/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ez2find.backing;

import com.ez2find.manager.CountryManagerLocal;
import com.ez2find.manager.SiteManagerLocal;
import com.ez2find.manager.AreaManagerLocal;
import com.ez2find.manager.LocationManagerLocal;
import com.ez2find.manager.NickManagerBean;
import com.ez2find.model.Area;
import com.ez2find.model.Country;
import com.ez2find.model.Location;
import com.ez2find.model.Site;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author Owner
 */
public class SiteBean {

    @EJB
    private CountryManagerLocal ejbCountryManager;
    @EJB
    private SiteManagerLocal ejbSiteManager;
    @EJB
    private AreaManagerLocal ejbAreaManager;
    @EJB
    private LocationManagerLocal ejbLocations;
    private Country currentCountry;
    private Site current;
    private Location currentLocation;
    private Area area;
    private int pageCountries;
    private int pageSites;
    private int pageAreas;
    private int pageLocations;
    private boolean newCountry;
    private boolean newSite;
    private boolean newArea;
    private boolean newLocation;
    
    public List<Country> getCountrys() {
        return ejbCountryManager.listAll();
    }

    public Country getCurrentCountry() {
        if (currentCountry == null) {
            currentCountry = new Country();
        }
        return currentCountry;
    }

    public void setCurrentCountry(Country currentCountry) {
        this.currentCountry = currentCountry;
    }

    public Site getCurrent() {
        if (current == null) {
            current = new Site();
            current.setJobPrice(0);
        }
        return current;
    }

    public void setCurrent(Site current) {
        this.current = current;
    }

    public Location getCurrentLocation() {
        if (currentLocation == null) {
            currentLocation = new Location();
        }
        return currentLocation;
    }

    public void setCurrentLocation(Location currentLocation) {
        this.currentLocation = currentLocation;
    }

    public String removeCountry() {
        try {
            //SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
            ejbCountryManager.delete(currentCountry);
            currentCountry=null;
                newCountry=false;
                System.out.println("Country removed");
            FacesMessage msg = new FacesMessage("Country removed.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        } catch (Exception ex) {
            currentCountry=null;
                newCountry=false;
                FacesMessage msg = new FacesMessage("Country cannot remove.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
                Logger.getLogger(NickManagerBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public String removeSite() {
        try {
            //SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
            ejbSiteManager.delete(current);
            current=null;
            newSite=false;
            System.out.println("Site removed");
            FacesMessage msg = new FacesMessage("Site removed.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        } catch (Exception ex) {
            current=null;
            newSite=false;
            System.out.println("Site can not removed");
            FacesMessage msg = new FacesMessage("Site can not remove.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            Logger.getLogger(NickManagerBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public String removeArea() {
        try {
            //SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
            ejbAreaManager.delete(area);
            area=null;
            newArea=false;
            System.out.println("Area removed");
            FacesMessage msg = new FacesMessage("Area removed.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        } catch (Exception ex) {
            area=null;
            newArea=false;
            System.out.println("Area can not remove");
            FacesMessage msg = new FacesMessage("Area can not remove.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            Logger.getLogger(NickManagerBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

        public String removeLocation() {
        try {
            //SessionBean sessionBean = (SessionBean) SessionContext.getAttribute("sessionBean");
            ejbLocations.delete(currentLocation);
            currentLocation=null;
                newLocation=false;
                System.out.println("Location removed");
            FacesMessage msg = new FacesMessage("Location removed.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        } catch (Exception ex) {
            currentLocation=null;
                newLocation=false;
            System.out.println("Error removing location");
            FacesMessage msg = new FacesMessage("Error removing location.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            Logger.getLogger(NickManagerBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    public boolean getNewCountry() {
        return newCountry;
    }

    public void setNewCountry(boolean newCountry) {
        this.newCountry = newCountry;
    }

    public String saveCountry() {
        System.out.println("Saving changes..");
        try {
            if ((getCurrentCountry() != null) && (getCurrentCountry().getId() != null)) {
                ejbCountryManager.update(currentCountry);
                currentCountry=null;
                newCountry=false;
                System.out.println("Country updated correctly.");
                FacesMessage msg = new FacesMessage("Country updated correctly.");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            } else {
                ejbCountryManager.create(currentCountry);
                currentCountry=null;
                newCountry=false;
                System.out.println("Country created correctly.");
                FacesMessage msg = new FacesMessage("Country created correctly.");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }

        } catch (Exception ex) {
            FacesMessage msg = new FacesMessage("Error.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            Logger.getLogger(NickManagerBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public String saveSite() {
        System.out.println("Saving changes..");
        try {
            if ((getCurrent() != null) && (getCurrent().getId() != null)) {
                ejbSiteManager.update(current);
                current=null;
            newSite=false;
            System.out.println("Site updated correctly.");
                FacesMessage msg = new FacesMessage("Site updated correctly.");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            } else {
                current.setCountry(currentCountry);
                ejbSiteManager.create(current);
                current=null;
            newSite=false;
            System.out.println("Site created correctly.");
                FacesMessage msg = new FacesMessage("Site created correctly.");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }

        } catch (Exception ex) {
            FacesMessage msg = new FacesMessage("Error.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            Logger.getLogger(NickManagerBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public String saveArea() {
        System.out.println("Saving changes..");
        try {
            if ((getArea() != null) && (getArea().getId() != null)) {
                ejbAreaManager.update(area);
                area=null;
            newArea=false;
            System.out.println("Area updated correctly.");
                FacesMessage msg = new FacesMessage("Area updated correctly.");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            } else {
                area.setSite(current);
                ejbAreaManager.create(area);
                area=null;
            newArea=false;
            System.out.println("Area created correctly.");
                FacesMessage msg = new FacesMessage("Area created correctly.");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }

        } catch (Exception ex) {
            FacesMessage msg = new FacesMessage("Error.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            Logger.getLogger(NickManagerBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

        public String saveLocation() {
        System.out.println("Saving changes..");
        try {
            if ((getCurrentLocation() != null) && (getCurrentLocation().getId() != null)) {
                ejbLocations.update(currentLocation);
                currentLocation=null;
                newLocation=false;
            System.out.println("Location updated correctly.");
                FacesMessage msg = new FacesMessage("Location updated correctly.");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            } else {
                currentLocation.setArea(area);
                ejbLocations.create(currentLocation);
                currentLocation=null;
                newLocation=false;
            System.out.println("Location created correctly.");
                FacesMessage msg = new FacesMessage("Location created correctly.");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                return null;
            }

        } catch (Exception ex) {
            FacesMessage msg = new FacesMessage("Error.");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            Logger.getLogger(NickManagerBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    public boolean getNewSite() {
        return newSite;
    }

    public void setNewSite(boolean newSite) {
        this.newSite = newSite;
    }

    public boolean getNewArea() {
        return newArea;
    }

    public void setNewArea(boolean newArea) {
        this.newArea = newArea;
    }

    public boolean getNewLocation() {
        return newLocation;
    }

    public void setNewLocation(boolean newLocation) {
        this.newLocation = newLocation;
    }

    public Area getArea() {
        if (area == null) {
            area = new Area();
        }
        return area;
    }

    public void setArea(Area area) {
        this.area = area;
    }
    
    public List<Site> getCurrentCountrySites(){
        return ejbSiteManager.sitesCountry(currentCountry);
    }
    public List<Area> getCurrentSiteAreas(){
        return ejbAreaManager.areasSite(current);
    }
    public List<Location> getCurrentAreaLocations(){
        return ejbLocations.locationsArea(area);
    }

    public int getPageCountries() {
        return pageCountries;
    }

    public void setPageCountries(int pageCountries) {
        this.pageCountries = pageCountries;
    }

    public int getPageSites() {
        return pageSites;
    }

    public void setPageSites(int pageSites) {
        this.pageSites = pageSites;
    }

    public int getPageAreas() {
        return pageAreas;
    }

    public void setPageAreas(int pageAreas) {
        this.pageAreas = pageAreas;
    }

    public int getPageLocations() {
        return pageLocations;
    }

    public void setPageLocations(int pageLocations) {
        this.pageLocations = pageLocations;
    }

}
